package com.huanglj.taobaounion.presenter.implement;

import com.huanglj.taobaounion.model.api.Api;
import com.huanglj.taobaounion.model.domain.SelectedByCategory;
import com.huanglj.taobaounion.model.domain.SelectedPage;
import com.huanglj.taobaounion.presenter.ISelectedPagePresenter;
import com.huanglj.taobaounion.utils.LogUtil;
import com.huanglj.taobaounion.utils.RetrofitManager;
import com.huanglj.taobaounion.utils.UrlUtils;
import com.huanglj.taobaounion.view.ISelectedPageCallBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelectedPagePresenterImpl implements ISelectedPagePresenter {
    private static final String TAG = "SelectedPagePresenterIm";

    private final Api mApi;
    private ISelectedPageCallBack mCallBack;

    public SelectedPagePresenterImpl(){
        mApi = RetrofitManager.getInstance().getRetrofit().create(Api.class);
    }
    @Override
    public void getCategories() {
        onLoading();
        Call<SelectedPage> selectCategories = mApi.getSelectCategories();
        selectCategories.enqueue(new Callback<SelectedPage>() {
            @Override
            public void onResponse(Call<SelectedPage> call, Response<SelectedPage> response) {
                LogUtil.d(TAG,response.code()+"");
                if (response.code()== HttpURLConnection.HTTP_OK) {
                    if (mCallBack != null) {
                        String message = response.body().getData().toString();
                        LogUtil.d(TAG,"data----> " + message);
                        mCallBack.onCategoriesLoaded(response.body());
                    }
                }else {
                    onLoadError(null);
                }
            }

            @Override
            public void onFailure(Call<SelectedPage> call, Throwable t) {
                onLoadError(t.toString());
            }
        });
    }

    @Override
    public void getContentByCategories(SelectedPage.DataBean item) {
        onLoading();
        String url = UrlUtils.getSelectContent(item.getFavorites_id());
        Call<SelectedByCategory> selectByCategory = mApi.getSelectByCategory(url);
        LogUtil.d(TAG,url);
        selectByCategory.enqueue(new Callback<SelectedByCategory>() {
            @Override
            public void onResponse(Call<SelectedByCategory> call, Response<SelectedByCategory> response) {
                LogUtil.d(TAG,response.code()+"");
                if (response.code()==HttpURLConnection.HTTP_OK) {
                    if (mCallBack != null) {
                        mCallBack.onCategoryById(response.body().getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data());
                    }
                }else {
                    onLoadError(null);
                }
            }

            @Override
            public void onFailure(Call<SelectedByCategory> call, Throwable t) {
                onLoadError(null);
            }
        });
    }

    @Override
    public void reLoadContent() {

    }
    private void onLoading() {
        if (mCallBack != null) {
            mCallBack.onLoading();
        }
    }

    private void onLoadError(String mMsg) {
        if (mCallBack != null) {
            mCallBack.onError(mMsg);
        }
    }

    @Override
    public void registerViewCallBack(ISelectedPageCallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public void unregisterViewCallBack(ISelectedPageCallBack callBack) {
        mCallBack = null;
    }
}
